package com.broada.apm.util;

import java.util.Properties;

import org.apache.wink.server.handlers.HandlersChain;
import org.apache.wink.server.handlers.MessageContext;
import org.apache.wink.server.handlers.ResponseHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.broada.apm.ajax.Result;
import com.broada.apm.utils.ExceptionUtils;

public class MyResponseHandler implements ResponseHandler {
    private static final Logger LOG = LoggerFactory.getLogger(MyResponseHandler.class);
    @Override
    public void init(Properties props) {

    }

    @Override
    public void handleResponse(MessageContext context, HandlersChain chain) throws Throwable {
        Object responseEntity = context.getResponseEntity();
        if (responseEntity != null && responseEntity instanceof Result) {
            Result r = (Result) responseEntity;
            if (r.isErr()) {
                context.setResponseStatusCode(409);
            }
        }
        try {
            chain.doChain(context);
        } catch (Throwable e) {
            ExceptionUtils.error(LOG, "服务处理过程出错", e);
        }
    }

}
